package de.baumann.browser.views.tab;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Bundle;
import android.view.View;

import java.io.Serializable;

import de.baumann.browser.R;
import de.baumann.browser.utils.ViewUtil;
import de.baumann.browser.web.WindowUiController;


public class Tab implements Serializable {
    /**
     * 默认网站图标
     */
    private static Bitmap sDefaultFavicon;

    private long mId = -1;

    /**
     * WebView controller
     */
    WindowUiController mHomeController;

    Context mContext;


    private int mCaptureWidth;
    private int mCaptureHeight;

    private Bitmap mCapture;
    private boolean mInForeground;
    private static Paint sAlphaPaint = new Paint();


    static {
        sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        sAlphaPaint.setColor(Color.TRANSPARENT);
    }

    /**
     * 获取默认网页图标
     */

    private static synchronized Bitmap getDefaultFavicon(Context context) {
        if (sDefaultFavicon == null) {
            sDefaultFavicon = BitmapFactory.decodeResource(
                    context.getResources(), R.drawable.ic_home);
        }
        return sDefaultFavicon;
    }


    Tab(WindowUiController uiController, Bundle state) {
        mHomeController = uiController;
        mContext = mHomeController.getContext();

        mCaptureWidth = ViewUtil.getScreenSize(mContext).x;
        mCaptureHeight = ViewUtil.getScreenSize(mContext).y;
        if (getId() == -1) {
            mId = TabManager.getNextId();
        }
    }

    private void updateShouldCaptureThumbnails() {
        synchronized (Tab.this) {
            if (mCapture == null) {
                mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
                        Bitmap.Config.RGB_565);
                mCapture.eraseColor(Color.WHITE);
            }
        }
    }

    public long getId() {
        return mId;
    }


    //    /**
//     * Sets the WebView for this tab, correctly removing the old WebView from
//     * the container view.
//     */
//    public void setWebView(OdinWebView w) {
//        if (mMainView == w) {
//            return;
//        }
//        mMainView = w;
//    }

    /**
     * Destroy the tab's main WebView and subWindow if any
     */
    void destroy() {
//        if (mMainView != null) {
//            mMainView.destroy();
//            mMainView = null;
//        }
    }

    void putInForeground() {
        if (mInForeground) {
            return;
        }
        mInForeground = true;
    }

    void putInBackground() {
        if (!mInForeground) {
            return;
        }
        mInForeground = false;
    }

    /**
     * Return the main window of this tab. Note: if a tab is freed in the
     * background, this can return null. It is only guaranteed to be
     * non-null for the current tab.
     *
     * @return The main WebView of this tab.
     */
//    public OdinWebView getWebView() {
//        return mMainView;
//    }

    /**
     * Get the title of this tab.
     */

    /**
     * Get the favicon of this tab.
     */
    public Bitmap getFavicon() {
        return getDefaultFavicon(mContext);
    }


    public Bitmap getScreenshot() {
        synchronized (Tab.this) {
            return mCapture;
        }
    }

    private String tabUrl;
    private String tabTitle;

    public void setTabTitle(String tabTitle) {
        this.tabTitle = tabTitle;
    }

    public void setTabUrl(String tabUrl) {
        this.tabUrl = tabUrl;
    }

    public String getTabUrl() {
        return tabUrl;
    }

    public String getTabTitle() {
        return tabTitle;
    }

    void capture2() {
        if (mCapture == null) {
            updateShouldCaptureThumbnails();
        }
        View view = ((Activity) mHomeController.getContext()).getWindow().getDecorView();
        mCapture = Bitmap.createScaledBitmap(mCapture, mCaptureWidth, mCaptureHeight, true);
        Canvas c = new Canvas(mCapture);
        int state = c.save();
//        if (!isBlank()) {
        c.translate(0, mContext.getResources().getDimensionPixelSize(R.dimen.dimen_48dp));
//        }
        view.draw(c);
        c.restoreToCount(state);
        c.setBitmap(null);
    }
}
